PREFIX ?= /usr/local
DEBUG ?= 0
STATIC ?= 0
CFLAGS += -I./include -std=gnu++11
LIBS += -lfftw3 -lfftw3_threads -lfftw3f -lfftw3f_threads -lfftw3l -lfftw3l_threads -lsndfile

ifeq ($(DEBUG),0)
CFLAGS += -O2 -DNDEBUG
else
CFLAGS += -O0 -g
endif

ifeq ($(OS),Windows_NT)
SO := dll
MACHINE := $(if $(find AMD64,$(PROCESSOR_ARCHITECTURE)),x86_64,i386)
else
SYSTEM := $(shell uname -s)
MACHINE := $(shell uname -m)
ifeq ($(SYSTEM),Darwin)
SO := dylib
CFLAGS += -I/opt/local/include
LIBS += -L/opt/local/lib 
else
SO := so
endif
endif

LIBDIR.x86_64 := lib64
LIBDIR.i386 := lib
LIBDIR := $(LIBDIR.$(MACHINE))

ifeq ($(STATIC),0)
CFLAGS += -fPIC -DDSPXX_EXPORTS -fvisibility=hidden 
LINKFLAG := -shared
EXT := $(SO)
else
EXT := a
endif

PRODUCT := libdsp++.$(EXT)
CXXFLAGS := $(CFLAGS)

SRC=src/debug.cpp src/fft.cpp src/filter.cpp src/fixed.cpp src/flt_biquad.cpp \
	src/flt_fs.cpp src/flt_iir.cpp src/flt_pm.cpp src/resample.cpp src/sample.cpp \
	src/simd.cpp src/vectmath.cpp src/zeropole.cpp src/arch/x86/cpu_x86.cpp \
	src/arch/x86/sse.cpp src/arch/x86/sse3.cpp src/arch/x86/sse41.cpp \
	src/fftw/traits.cpp src/mkfilter/mkfilter.cpp src/remez/remez.cpp \
	src/rpoly/rpoly.cpp src/snd/format.cpp src/snd/io.cpp src/snd/loudness.cpp \
	src/arch/arm/cpu_arm.cpp

OBJ := $(patsubst %.cpp,%.o, $(SRC))

all: $(PRODUCT)

%.o: %.cpp
	$(CXX) -c -o $@ $< $(CXXFLAGS)

src/arch/x86/sse.o: src/arch/x86/sse.cpp
	$(CXX) -c -o $@ $< $(CXXFLAGS) -msse

src/arch/x86/sse3.o: src/arch/x86/sse3.cpp
	$(CXX) -c -o $@ $< $(CXXFLAGS) -msse3

src/arch/x86/sse41.o: src/arch/x86/sse41.cpp
	$(CXX) -c -o $@ $< $(CXXFLAGS) -msse4.1

libdsp++.a: $(OBJ)
	ar cr $@ $?

libdsp++.$(SO): $(OBJ)
	$(CXX) -shared -Wl,-soname,$@ -o $@ $^ $(CXXFLAGS) $(LIBS)

.PHONY: clean docs install uninstall clean-docs distclean all

docs: doxyfile
	doxygen doxyfile

uninstall:
	-rm $(PREFIX)/$(LIBDIR)/$(PRODUCT)
	-rm -r $(PREFIX)/include/dsp++
	-rm -r $(PREFIX)/share/doc/dsp++/html

install: all docs
	mkdir -p $(PREFIX)/$(LIBDIR)
	cp $(PRODUCT) $(PREFIX)/$(LIBDIR)/$(PRODUCT)
	mkdir -p $(PREFIX)/include
	cp -r include/dsp++ $(PREFIX)/include/dsp++
	mkdir -p $(PREFIX)/share/doc/dsp++
	cp -r doc/html $(PREFIX)/share/doc/dsp++/html

clean:
	-rm $(OBJ) $(PRODUCT)

clean-docs:
	-rm -r doc

distclean: clean clean-docs
	

